package com.kkkk.rest.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 正则表达式
 *
 * @author czl
 * @date 2018/04/10
 */
public class RegexUtils {

    private static Pattern emailRegex = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$", Pattern.CASE_INSENSITIVE);
    private static Pattern chineseMobileRegex = Pattern.compile("^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[0-9]|19[0-9])[0-9]{8}$", Pattern.CASE_INSENSITIVE);
    private static Pattern dbMobileRegex = Pattern.compile("^(86-)(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[0-9]|19[0-9])[0-9]{8}$", Pattern.CASE_INSENSITIVE);
    private static Pattern usernameRegex = Pattern.compile("^[a-zA-Z0-9_]{6,40}$", Pattern.CASE_INSENSITIVE);
    private static Pattern passwordRegex = Pattern.compile("(?!.*[\\u4E00-\\u9FA5\\s])(?!^[a-zA-Z]$)(?!^[\\d]$)(?!^[^a-zA-Z\\d]$)^.{6,16}$", Pattern.CASE_INSENSITIVE);
    private static Pattern ipRegex = Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}", Pattern.CASE_INSENSITIVE);
    private static Pattern base64Regex = Pattern.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$", Pattern.CASE_INSENSITIVE);
    private static Pattern nameRegex = Pattern.compile("^[A-Za-z0-9_!@#$%^&*()\\u4e00-\\u9fa5]+$", Pattern.CASE_INSENSITIVE);
    private static Pattern numRegex = Pattern.compile("^[-\\\\+]?[\\\\d]*$", Pattern.CASE_INSENSITIVE);
    private static Pattern charOrNumRegex = Pattern.compile("^[A-Za-z0-9]+$", Pattern.CASE_INSENSITIVE);
    private static Pattern typeRegex = Pattern.compile("^[A-Za-z0-9_-]+$", Pattern.CASE_INSENSITIVE);

    private static final int MOBILE_LENGTH = 11;
    private static final int MAX_ADDRESS_LENGTH = 15;
    private static final int MIN_ADDRESS_LENGTH = 7;

    /**
     * 判断是否是邮箱
     *
     * @param str
     * @return
     */
    public static boolean isEmail(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = emailRegex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断中国手机号码
     *
     * @param phone
     * @return
     */
    public static boolean isChineseMobile(String phone) {
        String str = truncatPhone(phone);
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = chineseMobileRegex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static String toDBMobile(String mobile) {
        if (mobile.length() == MOBILE_LENGTH) {
            return "86-" + mobile;
        } else if (mobile.length() > MOBILE_LENGTH) {
            return "86-" + mobile.substring(mobile.length() - MOBILE_LENGTH);
        } else {
            return mobile;
        }
    }

    /**
     * 数据库最终存的手机号码
     *
     * @param str
     * @return
     */
    public static boolean isDbMobile(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = dbMobileRegex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断用户名规则是否合法
     *
     * @param str
     * @return
     */
    public static boolean checkUsername(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = usernameRegex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断密码规则是否合法，6-16位的数字、字母或符号（不能有中文和空格）
     *
     * @param str
     * @return
     */
    public static boolean checkPassword(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = passwordRegex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断ip格式是否正确
     *
     * @param addr
     * @return
     */
    public static boolean isIp(String addr) {
        if ("".equals(addr) || addr.length() < MIN_ADDRESS_LENGTH || addr.length() > MAX_ADDRESS_LENGTH) {
            return false;
        }
        Matcher matcher = ipRegex.matcher(addr);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是base64编码
     *
     * @param str
     * @return
     */
    public static boolean isBase64(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = base64Regex.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断通用名称规则是否合法，字母、数字、特殊字符、中文
     *
     * @param str
     * @return
     */
    public static boolean checkName(String str, Integer length) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Matcher matcher = nameRegex.matcher(str);
        if (!matcher.matches()) {
            return false;
        }
        String chinese = "[\u0391-\uFFE5]";
        int len = 0;
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chinese)) {
                len += 2;
            } else {
                len += 1;
            }
        }
        if (len > length) {
            return false;
        }
        return true;
    }

    /**
     * @Author Xun Chen
     * @Description :判断字符串是否为纯数字
     * @Date 19:52 2018/5/23
     **/
    public static boolean checkNum(String str, Integer length) {
        if (str == null || str.length() == 0) {
            return false;
        }
        if (str.length() > length) {
            return false;
        }
        Matcher matcher = numRegex.matcher(str);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断描述类字符串长度
     *
     * @param str
     * @return
     */
    public static boolean checkStrLength(String str, Integer length) {
        if (str == null || str.length() == 0) {
            return false;
        }
        if (str.length() > length) {
            return false;
        }
        return true;
    }

    /**
     * @Author Xun Chen
     * @Description :判断字符串是否为字母和数字
     * @Date 22:46 2018/6/21
     **/
    public static boolean checkCharOrNum(String str, Integer length) {
        if (str == null || str.length() == 0) {
            return false;
        }
        if (str.length() > length) {
            return false;
        }
        Matcher matcher = charOrNumRegex.matcher(str);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * @Author Xun Chen
     * @Description :判断字符串是否为字母数字下划线横杠
     * @Date 22:47 2018/6/21
     **/
    public static boolean checkType(String str, Integer length) {
        if (str == null || str.length() == 0) {
            return false;
        }
        if (str.length() > length) {
            return false;
        }
        Matcher matcher = typeRegex.matcher(str);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * @Author mj
     * @Description : 自定义密码长度
     * @Date 2019/12/20
     */
    public static boolean checkUserPasswordLength(String str, int start, int end) {
        if (str == null || str.length() == 0) {
            return false;
        }
        String chinese = "[\u0391-\uFFE5]";
        int len = 0;
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chinese)) {
                len += 2;
            } else {
                len += 1;
            }
        }
        if (len > end || len < start) {
            return false;
        }
        return true;
    }

    /**
     * 去除86-
     *
     * @param phone
     * @return
     */
    public static String truncatPhone(String phone) {
        int start = phone.indexOf("-");
        if (phone.contains("-")) {
            return phone.substring(start + 1);
        } else {
            return phone;
        }
    }

}